package cn.stylefeng.guns.modular.taskinfo.controller;

import cn.stylefeng.guns.modular.taskinfo.entity.AnswerRecord;
import cn.stylefeng.guns.modular.taskinfo.entity.TaskInfo;
import cn.stylefeng.guns.modular.taskinfo.entity.utils.CosUtils;
import cn.stylefeng.guns.modular.taskinfo.service.TaskService;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.util.List;


/**
 * _*_cding : utf-8 _*_
 *
 * @Time : 2023/4/22 14:56
 * @Author : Sen
 * @File : TaskInfoController
 * @Project: demo-projects-master
 */
@Log4j2
@Controller
@RequestMapping("/taskInfo")
public class TaskInfoController {
    private final TaskService taskService;

    private final HttpServletRequest request;


    public TaskInfoController(TaskService taskService, HttpServletRequest request) {
        this.taskService = taskService;
        this.request = request;
    }

    /**
     * 根据模块ID 查询相对应的任务
     * @param moduleId
     * @return
     */
    @ResponseBody
    @PostMapping("/taskInfoList/{moduleId}")
    public List<TaskInfo> taskList(@PathVariable("moduleId")Integer moduleId){
        String requestURI = request.getRequestURI();
        String method = request.getMethod();
        log.info("方法名称:[{}],请求路径:[{}],请求方式:[{}],请求参数:[{}]","根据模块ID 查询相对应的任务",requestURI,method,moduleId);
        List<TaskInfo> taskInfoList = taskService.taskList(moduleId);
        return taskInfoList;
    }

    /**
     * 根据任务ID 查询相对应的题
     * @param taskId
     * @return
     */
    @ResponseBody
    @PostMapping("/taskInfoQuestion/{taskId}")
    public List<TaskInfo> taskInfoQuestion(@PathVariable("taskId")Integer taskId){
        String requestURI = request.getRequestURI();
        String method = request.getMethod();
        log.info("方法名称:[{}],请求路径:[{}],请求方式:[{}],请求参数:[{}]","根据任务ID 查询相对应的题",requestURI,method,taskId);
        List<TaskInfo> taskInfoQuestion = taskService.taskInfoQuestion(taskId);
        return taskInfoQuestion;
    }

    /**
     * 插入答题记录
     * @param userId
     * @param taskId
     * @param answerRecord
     * @return
     */
    @ResponseBody
    @PostMapping("/insetAnswerRecord")
    public Boolean insert(Integer userId, Integer taskId, AnswerRecord answerRecord){
        taskService.insert(userId,taskId,answerRecord);
        return true;
    }


    /**
     * 图片上传
     * @param file
     * @return
     */
    @ResponseBody
    @PostMapping("/uploadPicture")
    public ResponseData uploadPicture(@RequestParam("file") MultipartFile file, Integer teamId) throws Exception {
        Part filePart = request.getPart("file");
        String fileName = filePart.getSubmittedFileName();
        log.info("图片名字："+fileName);
        String ext = fileName.substring(fileName.lastIndexOf("."));
        log.info("图片后缀："+ext);
        String newFileName = teamId+fileName;
        log.info("图片新名字："+newFileName);
        //        String newName = teamId
        //        File oldFile = new File(fileName); // 要重命名的文件
        //        File newFile = new File(newFileName); // 新的文件名或路径
        //        oldFile.renameTo(newFile); // 重命名操作
        //        log.info("图片新名字2："+filePart.getSubmittedFileName());
        /*filePart.write(newFileName);
        log.info("图片新名字2："+filePart.getSubmittedFileName());*/
        String uploadfile = CosUtils.uploadfile(file,teamId);
        taskService.insertFlie(teamId,uploadfile);
        return ResponseData.success(uploadfile);
    }
}
